package services;

import dao.ArticleInfoDao;
import models.vo.ArticleInfoVO;
import utils.DBUtils;
import utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/11
 * Time:13:44
 **/
@WebServlet("/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int succ = -1;
        String msg = "";
        List<ArticleInfoVO> list = null;
        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("size"));
        ArticleInfoDao articleInfoDao = new ArticleInfoDao();
        try {
            list = articleInfoDao.getListByPage(page,size);
            succ = 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        HashMap<String,Object> res = new HashMap<>();
        res.put("succ",succ);
        res.put("msg",msg);
        res.put("list",list);
        ResultUtils.write(resp,res);
    }
}
